<?php

// Include shared script

require_once('shared.php');


// Prepare database statement to update records from CSV file

$stmt1 = $db->prepare(
	'UPDATE		sherpa_importData
	SET			iconIds = ?,
				sizeTableId = ?,
				sizeInfo = ?,
				colorSet1 = ?,
				colorCode1 = ?,
				colorName1 = ?,
				rgbRed1 = ?,
				rgbGreen1 = ?,
				rgbBlue1 = ?,
				colorSet2 = ?,
				colorCode2 = ?,
				colorName2 = ?,
				rgbRed2 = ?,
				rgbGreen2 = ?,
				rgbBlue2 = ?,
				ready = 1
	WHERE		articleNumber = ?
	AND			eanCode IS NULL'
);

$stmt2 = $db->prepare(
	'UPDATE		sherpa_importData
	SET			iconIds = ?,
				sizeTableId = ?,
				sizeInfo = ?,
				colorSet1 = ?,
				colorCode1 = ?,
				colorName1 = ?,
				rgbRed1 = ?,
				rgbGreen1 = ?,
				rgbBlue1 = ?,
				colorSet2 = ?,
				colorCode2 = ?,
				colorName2 = ?,
				rgbRed2 = ?,
				rgbGreen2 = ?,
				rgbBlue2 = ?,
				ready = 1
	WHERE		articleNumber = ?
	AND			eanCode = ?'
);

$stmt3 = $db->prepare(
	'SELECT		articleNumber,
				count( * ) AS num
				FROM sherpa_importData
				WHERE ready = 1
				GROUP BY articleNumber
				HAVING num = 1'
);

$stmt4 = $db->prepare(
	'UPDATE		sherpa_importData
	SET			ready = 0
	WHERE		articleNumber = ?'
);

$stmt5 = $db->prepare(
	'UPDATE		sherpa_importData
	SET			size = "-"
	WHERE		eanCode IS NOT NULL
	AND			size IS NULL'
);


// Process CSV file

$fh = fopen('../data/ProduktdatenManuell.csv','r');

$i = 0;
$fieldNames = array();

while (($data = fgetcsv($fh, null, ';')) !== false ) {

	if ($i == 0) {
		$fieldNames = $data;
	}

	else {
		$data = array_combine($fieldNames, $data);
		
		if ($data['eanCode'] == null) {
			$stmt1->execute(array(
				nullify($data['icons']),
				nullify($data['sizeTableId']),
				nullify($data['sizeInfo']),
				nullify($data['colorSet1']),
				nullify($data['colorCode1']),
				nullify($data['colorName1']),
				nullify($data['rgbRed1']),
				nullify($data['rgbGreen1']),
				nullify($data['rgbBlue1']),
				nullify($data['colorSet2']),
				nullify($data['colorCode2']),
				nullify($data['colorName2']),
				nullify($data['rgbRed2']),
				nullify($data['rgbGreen2']),
				nullify($data['rgbBlue2']),
				nullify($data['articleNumber'])
			));
		}
		else {
			$stmt2->execute(array(
				nullify($data['icons']),
				nullify($data['sizeTableId']),
				nullify($data['sizeInfo']),
				nullify($data['colorSet1']),
				nullify($data['colorCode1']),
				nullify($data['colorName1']),
				nullify($data['rgbRed1']),
				nullify($data['rgbGreen1']),
				nullify($data['rgbBlue1']),
				nullify($data['colorSet2']),
				nullify($data['colorCode2']),
				nullify($data['colorName2']),
				nullify($data['rgbRed2']),
				nullify($data['rgbGreen2']),
				nullify($data['rgbBlue2']),
				nullify($data['articleNumber']),
				nullify($data['eanCode'])
			));
		}
	}

	$i++;
}

fclose($fh);


// Mark products with no parent or child as not ready to be imported

$stmt3->execute();

while ($row = $stmt3->fetch(PDO::FETCH_ASSOC)) {
	$stmt4->execute(array(
		$row['articleNumber']
	));
}


// Assign default size to children products that have no size
// (temporary solution)

$stmt5->execute();